package part12cas;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @description: AtomicReference使用
 * @author: zhang hang
 * @date: 2024/3/24 17:28.
 * @version: 1.0
 */
public class CASTest02 {

    public static void main(String[] args) {
        User zhangsan = new User("zhangsan", 22);
        User lisi = new User("lisi", 25);
        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(zhangsan); // 设置
        System.out.print(atomicReference.compareAndSet(zhangsan, lisi));
        System.out.println(atomicReference.get().toString());
        System.out.print(atomicReference.compareAndSet(zhangsan, lisi));
        System.out.println(atomicReference.get().toString());
    }

}

class User {
    String username;
    int age;

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}